﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Dragon.Prototype1.Infrastructure.Controls
{
    public enum DatePickerShowOn
    {
        focus,
        both,
        button
    }


    public class DatePickerBox : TextBox
    {

        public DatePickerBox(string id)
            : this(id, "#" + id, DatePickerShowOn.focus, null, false)
        { 

        }

        public DatePickerBox(string id,string inputSelector,DatePickerShowOn showon, string imageUrl, bool buttonImageOnly):base(inputSelector)
        {
            this.IsReadOnly = true;
            this.ID = id;
            this.Selector = inputSelector;
            this.ShowOn = showon;
            this.ButtonImagePath = imageUrl;
            this.ButtonImageOnly = buttonImageOnly;

            //set default values
            this.DateFormat = "mm/dd/yy";
            this.CurrentButtonText = "Today";
            this.NextText = "Next";
            this.PreviousText = "Prev";
            this.ConstrainInput = true;
            this.NumberOfMonthsToShow = 1;
            this.MonthSteps = 1;
        }

        #region jQuery DatePicker Parameters
        /// <summary>
        /// Gets or sets the alternate field.
        /// </summary>
        /// <value>The alternate field.</value>
        [jQueryParameter("altField")]
        public string AlternateField
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the alternate field format.
        /// </summary>
        /// <value>The alternate field format.</value>
        [jQueryParameter("altFormat")]
        public string AlternateFieldFormat
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the append text.
        /// </summary>
        /// <value>The append text.</value>
        [jQueryParameter("appendText")]
        public string AppendText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the button text.
        /// </summary>
        /// <value>The button text.</value>
        [jQueryParameter("buttonText")]
        public string ButtonText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the button image path.
        /// </summary>
        /// <value>The button image path.</value>
        [jQueryParameter("buttonImage")]
        public string ButtonImagePath
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to button image only.
        /// </summary>
        /// <value><c>true</c> if [button image only]; otherwise, <c>false</c>.</value>
        [jQueryParameter("buttonImageOnly",false)]
        public bool ButtonImageOnly
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to allow month change.
        /// </summary>
        /// <value><c>true</c> if [allow month change]; otherwise, <c>false</c>.</value>
        [jQueryParameter("changeMonth",false)]
        public bool AllowMonthChange
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to allow year change.
        /// </summary>
        /// <value><c>true</c> if [allow year change]; otherwise, <c>false</c>.</value>
        [jQueryParameter("changeYear",false)]
        public bool AllowYearChange
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the close button text.
        /// </summary>
        /// <value>The close button text.</value>
        [jQueryParameter("closeText")]
        public string CloseButtonText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the current button text.
        /// </summary>
        /// <value>The current button text.</value>
        [jQueryParameter("currentText","Today")]
        public string CurrentButtonText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to go to current.
        /// </summary>
        /// <value><c>true</c> if [go to current]; otherwise, <c>false</c>.</value>
        [jQueryParameter("gotoCurrent",false)]
        public bool GoToCurrent
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to constrain input.
        /// </summary>
        /// <value><c>true</c> if [constrain input]; otherwise, <c>false</c>.</value>
        [jQueryParameter("constrainInput",true)]       
        public bool ConstrainInput
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the date format.
        /// </summary>
        /// <value>The date format.</value>
        [jQueryParameter("dateFormat","mm/dd/yy")]
        public string DateFormat
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the maximum value.
        /// </summary>
        /// <value>The maximum value.</value>
        [jQueryParameter("maxDate")]
        public DateTime? MaximumValue
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the minimum value.
        /// </summary>
        /// <value>The minimum value.</value>
        [jQueryParameter("minDate")]
        public DateTime? MinimumValue
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the previous text.
        /// </summary>
        /// <value>The previous text.</value>
        [jQueryParameter("prevText","Prev")]
        public string PreviousText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the next text.
        /// </summary>
        /// <value>The next text.</value>
        [jQueryParameter("nextText","Next")]
        public string NextText
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to hide if no previous or next.
        /// </summary>
        /// <value>
        /// <c>true</c> if [hide if no previous or next]; otherwise, <c>false</c>.
        /// </value>
        [jQueryParameter("hideIfNoPrevNext",false)]
        public bool HideIfNoPreviousOrNext
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to navigation as date format.
        /// </summary>
        /// <value>
        /// <c>true</c> if [navigation as date format]; otherwise, <c>false</c>.
        /// </value>
        [jQueryParameter("navigationAsDateFormat",false)]
        public bool NavigationAsDateFormat
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the number of months to show.
        /// </summary>
        /// <value>The number of months to show.</value>
        [jQueryParameter("numberOfMonths",1)]
        public int NumberOfMonthsToShow
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to show month after year.
        /// </summary>
        /// <value><c>true</c> if [show month after year]; otherwise, <c>false</c>.</value>
        [jQueryParameter("showMonthAfterYear",false)]
        public bool ShowMonthAfterYear
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to show other months.
        /// </summary>
        /// <value><c>true</c> if [show other months]; otherwise, <c>false</c>.</value>
        [jQueryParameter("showOtherMonths",false)]
        public bool ShowOtherMonths
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether to show button panel.
        /// </summary>
        /// <value><c>true</c> if [show button panel]; otherwise, <c>false</c>.</value>
        [jQueryParameter("showButtonPanel",false)]
        public bool ShowButtonPanel
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the month steps.
        /// </summary>
        /// <value>The month steps.</value>
        [jQueryParameter("stepMonths",1)]
        public int MonthSteps
        {
            get;
            set;
        }

        [jQueryParameter("showOn",DatePickerShowOn.focus)]
        public DatePickerShowOn ShowOn
        {
            get;
            set;
        }
        #endregion

        public override void RenderJavaScriptContents(System.Web.UI.HtmlTextWriter writer)
        {
            base.RenderJavaScriptContents(writer);
            writer.WriteLine(string.Format("$(\"{0}\").datepicker({1});",this.Selector,GeneratejQueryParameterArray()));
        }
    }
}
